<?php 
namespace lib;
use DOMDocument;
use DOMXPath;
class CurlUrl{
    protected $api;
    protected $host;
    public function __construct(){
        $this->api = [
            ['url'=>"https://auth.daoh.top/"],
            ['url'=>"https://www.x-rates.com/calculator/?from=CNY&to=USD&amount="]
        ];
        $this->host = $_SERVER['HTTP_HOST'];
    }
    public function yiyan(){
        $api = $this->api[0]['url'] . 'api/yiyan';
        return get_curl($api);
    }
    public function ipcity($ip){
        $api = $this->api[0]['url'] . 'api/ip_city';
        $post=[
            'ip'=>$ip,
        ];
        $res = get_curl($api,$post);
        $json = json_decode($res,true);
        
        $data = [
            'pro'=>$json['data']['province'],
            'city'=>$json['data']['city1'],
            'ip'=>$ip,
            'details'=>$json['data']['city'],
            'segment'=>($json['data']['beginip'] && $json['data']['endip']) ?$json['data']['beginip'] . ' - ' . $json['data']['endip'] :''
        ];
        return $data;
    }
    public function imgdecrypt($url){
        $api = $this->api[0]['url'] . 'api/jiema';
        $res = get_curl($api,['url'=>$url]);
        $json = json_decode($res,true);
        if($json['code'] == 1){
            return ['code'=>$json['code'] , 'qrurl'=>$json['qrurl']];
        }
        return ['code'=>-1,'msg'=>$json['msg']?:"解码失败"];
    }
    public function qr_token($url , $token){
        $api = $this->api[0]['url'] .'api/qrcode?url='. $url . '|'.$token;
        
        $res = get_curl($api);
        return "data:image/jpg;base64,".base64_encode($res);
    }
    public function Obtain_UsdtMoney($money){
        $api = $this->api[1]['url'] . $money;
        $res=get_curl($api);
        $dom = new DOMDocument();
        @$dom->loadHTML($res);
        $xpath = new DOMXPath($dom);
        $elements = $xpath->query('//span[@class="ccOutputRslt"]');
        if ($elements->length > 0) {
            $rateText = $elements->item(0)->nodeValue;
            return round(floatval($rateText), 2);
        }else{
            return $money;
        }
    }
    
    public function ALIH5URL($Xf_MOSHI , $price , $userId , $trade_no){
        $param=base64_encode(authcode($userId."\t".$trade_no."\t".$price."\t".$Xf_MOSHI,'ENCODE','XFALIURLH5'));
        $res = $this->api[0]['url'] . 'api/aliurlh5?url=' . $param;
        $res = get_curl($res);
        $json = json_decode($res,true);
        $alipayh5url_4  = $json['code'] == 1 ? $json['url'] : ($json['msg'] ? $json['msg'] : '获取失败');
        return $alipayh5url_4;
    }
    public function b64_qrcode($qr_url = ''){
        if(empty($qr_url))return false;
        $url = $this->api[0]['url'] . 'api/qrcode?url='.$qr_url;
        $res = get_curl($url);
        return "data:image/jpg;base64,".base64_encode($res);
    }
    public function aliqrurl($userId , $trade_no , $price , $xf_moshi = 3){
        $param = base64_encode(authcode($userId . "\t" . $trade_no . "\t" . $price . "\t" . $xf_moshi, 'ENCODE', 'XFALIURLH5'));
        $api = $this->api[0]['url'] . 'api/aliqrurl?url=' . $param;
        $result = get_curl($api);
        // 解析返回的 JSON 数据
        $json = json_decode($result, true);
    
        // 根据返回结果设置 QR_row 变量的值
        $QR_row = $json ? ($json['code'] == '1' ? $json['url'] : $json['msg']) : '获取失败';
    
        return $QR_row;
    }
    public function qqmsurl($price ,$trade_no,$cookie){
        $api = $this->api[0]['url'] . 'api/qqms';
        $data = [
            'money'=>$price,
            'beizhu'=>$trade_no,
            'cookie'=>$cookie
        ];
        $url = $api . '?' .http_build_query($data);
        $res = get_curl($url);
        return $res;
    }
    public function usdtNotify($qr_url){
        $url = "https://usdt.tokenview.io/api/usdtsearch/" . $qr_url;
        $result = get_curl($url);
        $json   = json_decode($result,true);
        
        if($json['code'] == 1 && $json['data'][0]['balance'] !== false){
            return $json['data'][0]['balance'];
        }
        return false;
       
    }
    
    public function update_api_check($arr){
        $api = $this->api[0]['url'] . 'api_check.php';
        $data= [
            'url'=>$arr->authdata['host'],
            'authcode'=>$arr->authdata['authcode'],
            'ver'=>Version,
            'cxid'=>$arr->authdata['cxid']
        ];
        $http = $api . '?' .http_build_query($data);
        $res  = get_curl($http);
        $res  = json_decode($res,true);
        return $res;
    }
    public function update_log($arr){
        $api = $this->api[0]['url'] . 'gx.php';
        $data= [
            'act'=>"api",
            'url'=>$arr->authdata['host'],
            'cxid'=>$arr->authdata['cxid']
        ];
        $http = $api . '?' .http_build_query($data);
        $res  = get_curl($http);
        $res  = json_decode($res,true);
        return $res;
    }
}

?>